/*
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * RSVisualScenesLibrary.cpp
 *
 *  Created on: 2010/03/04
 *      Author: Alexandre Pires ( c.alexandre.pires@gmail.com )
 */

#include "RSVisualScenesLibrary.h"
#include "RSVisualScene.h"

namespace Realsphere {

    RSVisualScenesLibrary::RSVisualScenesLibrary ( const QString& rId, const QString& rName ) :
        RSUIDElement ( rId, RS_VISUAL_SCENES_LIBRARY_TAG )
    {
        SetName ( rName, false );
    }

    RSVisualScenesLibrary::~RSVisualScenesLibrary ()
    {
        mVisualScenes.clear ();
    }

    RSVisualScene* RSVisualScenesLibrary::GetVisualScene ( unsigned int rIndex ) const
    {
        return mVisualScenes[rIndex];
    }

    RSVisualScene* RSVisualScenesLibrary::GetVisualScene ( const QString& rId ) const
    {
        RSVisualScenesIterator iterator =
                RSVisualScenesIterator ( mVisualScenes );

        while ( iterator.hasNext () )
        {

            RSVisualScene* current_VisualScene = iterator.next ();
            QString VisualScene_id = current_VisualScene->Id ();

            if ( VisualScene_id == rId )
                return current_VisualScene;
        }

        return NULL;
    }

    int RSVisualScenesLibrary::IndexOf ( RSVisualScene* rVisualScene )
    {
        return mVisualScenes.indexOf ( rVisualScene );
    }

    unsigned int RSVisualScenesLibrary::VisualScenesSize ()
    {
        return mVisualScenes.size ();
    }

    RSVisualScenes RSVisualScenesLibrary::VisualScenes () const
    {
        return mVisualScenes;
    }

    RSVisualScenesIterator RSVisualScenesLibrary::VisualScenesIterator () const
    {
        return RSVisualScenesIterator ( mVisualScenes );
    }

    bool RSVisualScenesLibrary::IsRootElement () const
    {
        return false;
    }

    RSError RSVisualScenesLibrary::BeforeAddChildElement ( RSElement* rChild )
    {
        RSError error = RSElement::BeforeAddChildElement ( rChild );

        if ( error == RS_INVALID_ELEMENT )
        {

            if ( rChild->InstanceOf ( RS_VISUAL_SCENE_CLASS ) )
                error = RS_NO_ERROR;

        }

        return error;
    }

    bool RSVisualScenesLibrary::AfterAddChildElement ( RSElement* rChild )
    {
        bool handled = RSElement::AfterAddChildElement ( rChild );

        if ( !handled && rChild->InstanceOf ( RS_VISUAL_SCENE_CLASS ) )
        {
            RSVisualScene* visual_scene = RSVisualSceneCast ( rChild );
            mVisualScenes.append ( visual_scene );
            handled = true;

            emit VisualSceneAdded ( visual_scene );
        }

        return handled;
    }

    bool RSVisualScenesLibrary::AfterRemoveChildElement ( RSElement* rChild )
    {
        bool handled = RSElement::AfterRemoveChildElement ( rChild );

        if ( !handled && rChild->InstanceOf ( RS_VISUAL_SCENE_CLASS ) )
        {
            RSVisualScene* visual_scene = RSVisualSceneCast ( rChild );
            int index = mVisualScenes.indexOf ( visual_scene );
            mVisualScenes.removeAt ( index );
            handled = true;

            emit VisualSceneRemoved ( visual_scene );
        }

        return handled;
    }

    RSError RSVisualScenesLibrary::AddVisualScene ( RSVisualScene* rVisualScene )
    {
        RSError error = AddChildElement ( rVisualScene );
        return error;
    }

    RSError RSVisualScenesLibrary::RemoveVisualScene ( RSVisualScene* rVisualScene )
    {
        RSError error = RemoveChildElement ( rVisualScene );
        return error;
    }

    RSError RSVisualScenesLibrary::RemoveVisualScene ( const QString& rId )
    {
        RSVisualScene* VisualScene = GetVisualScene ( rId );
        RSError error = RemoveVisualScene ( VisualScene );
        return error;
    }

    RSError RSVisualScenesLibrary::RemoveVisualScene ( unsigned int rIndex )
    {
        RSVisualScene* VisualScene = GetVisualScene ( rIndex );
        RSError error = RemoveVisualScene ( VisualScene );
        return error;
    }

}
